<style type="text/css">
.highlight .hll { background-color: #ffffcc }
.highlight  { background: #f8f8f8; }
.highlight .c { color: #408080; font-style: italic } /* Comment */
.highlight .err { border: 1px solid #FF0000 } /* Error */
.highlight .k { color: #008000; font-weight: bold } /* Keyword */
.highlight .o { color: #666666 } /* Operator */
.highlight .cm { color: #408080; font-style: italic } /* Comment.Multiline */
.highlight .cp { color: #BC7A00 } /* Comment.Preproc */
.highlight .c1 { color: #408080; font-style: italic } /* Comment.Single */
.highlight .cs { color: #408080; font-style: italic } /* Comment.Special */
.highlight .gd { color: #A00000 } /* Generic.Deleted */
.highlight .ge { font-style: italic } /* Generic.Emph */
.highlight .gr { color: #FF0000 } /* Generic.Error */
.highlight .gh { color: #000080; font-weight: bold } /* Generic.Heading */
.highlight .gi { color: #00A000 } /* Generic.Inserted */
.highlight .go { color: #888888 } /* Generic.Output */
.highlight .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
.highlight .gs { font-weight: bold } /* Generic.Strong */
.highlight .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
.highlight .gt { color: #0044DD } /* Generic.Traceback */
.highlight .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
.highlight .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
.highlight .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
.highlight .kp { color: #008000 } /* Keyword.Pseudo */
.highlight .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
.highlight .kt { color: #B00040 } /* Keyword.Type */
.highlight .m { color: #666666 } /* Literal.Number */
.highlight .s { color: #BA2121 } /* Literal.String */
.highlight .na { color: #7D9029 } /* Name.Attribute */
.highlight .nb { color: #008000 } /* Name.Builtin */
.highlight .nc { color: #0000FF; font-weight: bold } /* Name.Class */
.highlight .no { color: #880000 } /* Name.Constant */
.highlight .nd { color: #AA22FF } /* Name.Decorator */
.highlight .ni { color: #999999; font-weight: bold } /* Name.Entity */
.highlight .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
.highlight .nf { color: #0000FF } /* Name.Function */
.highlight .nl { color: #A0A000 } /* Name.Label */
.highlight .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
.highlight .nt { color: #008000; font-weight: bold } /* Name.Tag */
.highlight .nv { color: #19177C } /* Name.Variable */
.highlight .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
.highlight .w { color: #bbbbbb } /* Text.Whitespace */
.highlight .mb { color: #666666 } /* Literal.Number.Bin */
.highlight .mf { color: #666666 } /* Literal.Number.Float */
.highlight .mh { color: #666666 } /* Literal.Number.Hex */
.highlight .mi { color: #666666 } /* Literal.Number.Integer */
.highlight .mo { color: #666666 } /* Literal.Number.Oct */
.highlight .sb { color: #BA2121 } /* Literal.String.Backtick */
.highlight .sc { color: #BA2121 } /* Literal.String.Char */
.highlight .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
.highlight .s2 { color: #BA2121 } /* Literal.String.Double */
.highlight .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
.highlight .sh { color: #BA2121 } /* Literal.String.Heredoc */
.highlight .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
.highlight .sx { color: #008000 } /* Literal.String.Other */
.highlight .sr { color: #BB6688 } /* Literal.String.Regex */
.highlight .s1 { color: #BA2121 } /* Literal.String.Single */
.highlight .ss { color: #19177C } /* Literal.String.Symbol */
.highlight .bp { color: #008000 } /* Name.Builtin.Pseudo */
.highlight .vc { color: #19177C } /* Name.Variable.Class */
.highlight .vg { color: #19177C } /* Name.Variable.Global */
.highlight .vi { color: #19177C } /* Name.Variable.Instance */
.highlight .il { color: #666666 } /* Literal.Number.Integer.Long */
div.inner_cell{display:-webkit-box;-webkit-box-orient:vertical;-webkit-box-align:stretch;display:-moz-box;-moz-box-orient:vertical;-moz-box-align:stretch;display:box;box-orient:vertical;box-align:stretch;display:flex;flex-direction:column;align-items:stretch;-webkit-box-flex:1;-moz-box-flex:1;box-flex:1;flex:1}
div.input_area{border:1px solid #cfcfcf;border-radius:4px;background:#f7f7f7;line-height:1.21429em}
div.input_area>div.highlight{margin:.4em;border:none;padding:0;background-color:transparent}
div.input_area>div.highlight>pre{margin:0;border:none;padding:0;background-color:transparent}
pre { font-size: 11pt; }
    </style>

<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We want to solve the heat equation in 1D, <span class="math">\(ρ c_t \dot{u} - k u,_{xx} = 0\)</span>, using NuTo. A very simple analytical solution, which we're going to use to supply the Dirichlet boundary conditions, and to compare the accuracy of our solution, is <span class="math">\(u = 1 + x^2 + 2t\)</span>. Maybe we can define this first, to get some code in early:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[1]:
</div>
<div class="inner_cell">
    <div class="input_area">
<div class="highlight"><pre><span class="k">def</span> <span class="nf">analytic_solution</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">t</span><span class="o">=</span><span class="mf">0.0</span><span class="p">):</span>
    <span class="k">return</span> <span class="mf">1.0</span> <span class="o">+</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="mf">2.0</span><span class="o">*</span><span class="n">t</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>To do any actual calculations, we need to import Numpy and NuTo:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[2]:
</div>
<div class="inner_cell">
    <div class="input_area">
<div class="highlight"><pre><span class="kn">import</span> <span class="nn">numpy</span> <span class="kn">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">nuto</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Let's define the length of our domain, as well as the material parameters:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[3]:
</div>
<div class="inner_cell">
    <div class="input_area">
<div class="highlight"><pre><span class="c"># Geometry</span>
<span class="n">length</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="c"># Material</span>
<span class="n">conductivity</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">capacity</span> <span class="o">=</span> <span class="mf">1.0</span>
<span class="n">density</span> <span class="o">=</span> <span class="mf">1.0</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>First we need to create a structure. I'll pass the number of time derivatives as a parameter, because I want create structures for both the transient and the static problem:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[4]:
</div>
<div class="inner_cell">
    <div class="input_area">
<div class="highlight"><pre><span class="k">def</span> <span class="nf">create_structure</span><span class="p">(</span><span class="n">number_of_time_derivatives</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
    <span class="c"># Geometry/Mesh</span>
    <span class="n">area</span> <span class="o">=</span> <span class="mf">1.0</span>
    <span class="n">number_of_elements</span> <span class="o">=</span> <span class="mi">20</span>

    <span class="c"># create one-dimensional structure</span>
    <span class="n">structure</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">Structure</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">SetNumTimeDerivatives</span><span class="p">(</span><span class="n">number_of_time_derivatives</span><span class="p">)</span>

    <span class="c"># create section</span>
    <span class="n">section</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">SectionCreate</span><span class="p">(</span><span class="s">&quot;Truss&quot;</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">SectionSetArea</span><span class="p">(</span><span class="n">section</span><span class="p">,</span> <span class="n">area</span><span class="p">)</span>

    <span class="c"># create material law</span>
    <span class="n">material</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">ConstitutiveLawCreate</span><span class="p">(</span><span class="s">&quot;Heat_Conduction&quot;</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">ConstitutiveLawSetParameterDouble</span><span class="p">(</span><span class="n">material</span><span class="p">,</span> <span class="s">&quot;Thermal_Conductivity&quot;</span><span class="p">,</span> <span class="n">conductivity</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">ConstitutiveLawSetParameterDouble</span><span class="p">(</span><span class="n">material</span><span class="p">,</span> <span class="s">&quot;Heat_Capacity&quot;</span><span class="p">,</span> <span class="n">capacity</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">ConstitutiveLawSetParameterDouble</span><span class="p">(</span><span class="n">material</span><span class="p">,</span> <span class="s">&quot;Density&quot;</span><span class="p">,</span> <span class="n">density</span><span class="p">)</span>

    <span class="c"># create nodes</span>
    <span class="n">node_coordinates</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">DoubleFullVector</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">node</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">number_of_elements</span> <span class="o">+</span> <span class="mi">1</span><span class="p">):</span>
        <span class="n">node_coordinates</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">node</span> <span class="o">*</span> <span class="n">length</span><span class="o">/</span><span class="n">number_of_elements</span><span class="p">)</span>
        <span class="n">structure</span><span class="o">.</span><span class="n">NodeCreate</span><span class="p">(</span><span class="n">node</span><span class="p">,</span> <span class="n">node_coordinates</span><span class="p">)</span>

    <span class="c"># create interpolation type</span>
    <span class="n">truss_interpolation</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">InterpolationTypeCreate</span><span class="p">(</span><span class="s">&quot;Truss1D&quot;</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">InterpolationTypeAdd</span><span class="p">(</span><span class="n">truss_interpolation</span><span class="p">,</span> <span class="s">&quot;coordinates&quot;</span><span class="p">,</span> <span class="s">&quot;equidistant1&quot;</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">InterpolationTypeAdd</span><span class="p">(</span><span class="n">truss_interpolation</span><span class="p">,</span> <span class="s">&quot;temperature&quot;</span><span class="p">,</span> <span class="s">&quot;equidistant1&quot;</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">InterpolationTypeSetIntegrationType</span><span class="p">(</span><span class="n">truss_interpolation</span><span class="p">,</span> <span class="s">&quot;1D2NGauss2Ip&quot;</span><span class="p">,</span> <span class="s">&quot;noipdata&quot;</span><span class="p">)</span>

    <span class="c"># create elements</span>
    <span class="n">element_incidence</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">IntFullVector</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">number_of_elements</span><span class="p">):</span>
        <span class="n">element_incidence</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">element</span><span class="p">)</span>
        <span class="n">element_incidence</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">element</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">structure</span><span class="o">.</span><span class="n">ElementCreate</span><span class="p">(</span><span class="n">truss_interpolation</span><span class="p">,</span> <span class="n">element_incidence</span><span class="p">)</span>
        <span class="n">structure</span><span class="o">.</span><span class="n">ElementSetSection</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="n">section</span><span class="p">)</span>
        <span class="n">structure</span><span class="o">.</span><span class="n">ElementSetConstitutiveLaw</span><span class="p">(</span><span class="n">element</span><span class="p">,</span> <span class="n">material</span><span class="p">)</span>

    <span class="n">structure</span><span class="o">.</span><span class="n">ElementTotalConvertToInterpolationType</span><span class="p">()</span>

    <span class="c"># visualize results</span>
    <span class="n">visualization_group</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">GroupCreate</span><span class="p">(</span><span class="s">&quot;Elements&quot;</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">GroupAddElementsTotal</span><span class="p">(</span><span class="n">visualization_group</span><span class="p">)</span>

    <span class="n">structure</span><span class="o">.</span><span class="n">AddVisualizationComponent</span><span class="p">(</span><span class="n">visualization_group</span><span class="p">,</span> <span class="s">&quot;Temperature&quot;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">structure</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We'll compute a static solution with the boundary conditions <span class="math">\(u(x=0) = 0\)</span> and <span class="math">\(q(x=l) = 10.0\)</span>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[5]:
</div>
<div class="inner_cell">
    <div class="input_area">
<div class="highlight"><pre><span class="k">def</span> <span class="nf">static_solve</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>
     <span class="c"># Boundaries</span>
    <span class="n">boundary_temperature</span> <span class="o">=</span> <span class="mf">0.0</span>
    <span class="n">boundary_flux</span> <span class="o">=</span> <span class="mf">10.0</span>

    <span class="c"># set dirichlet boundary condition</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">ConstraintLinearSetTemperatureNode</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">boundary_temperature</span><span class="p">)</span>

    <span class="c"># set Neumann bc</span>
    <span class="n">last_node_id</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">GetNumNodes</span><span class="p">()</span> <span class="o">-</span> <span class="mi">1</span>
    <span class="n">direction</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">DoubleFullMatrix</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">1</span><span class="p">,))</span>
    <span class="n">direction</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">1.0</span><span class="p">)</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">LoadCreateNodeHeatFlux</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">last_node_id</span><span class="p">,</span> <span class="n">direction</span><span class="p">,</span> <span class="n">boundary_flux</span><span class="p">)</span>

    <span class="c"># start analysis</span>
    <span class="n">structure</span><span class="o">.</span><span class="n">SolveGlobalSystemStaticElastic</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="n">int_gradient_tmp</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">BuildGlobalInternalGradient</span><span class="p">()</span>
    <span class="n">int_gradient</span> <span class="o">=</span> <span class="n">int_gradient_tmp</span><span class="o">.</span><span class="n">J</span><span class="o">.</span><span class="n">Get</span><span class="p">(</span><span class="s">&quot;Temperature&quot;</span><span class="p">)</span>
    <span class="n">ext_gradient_tmp</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">BuildGlobalExternalLoadVector</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
    <span class="n">ext_gradient</span> <span class="o">=</span> <span class="n">ext_gradient_tmp</span><span class="o">.</span><span class="n">J</span><span class="o">.</span><span class="n">Get</span><span class="p">(</span><span class="s">&quot;Temperature&quot;</span><span class="p">)</span>
    <span class="n">residual</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">DoubleFullVector</span><span class="p">(</span><span class="n">int_gradient</span> <span class="o">-</span> <span class="n">ext_gradient</span><span class="p">)</span>
    <span class="k">print</span><span class="p">(</span><span class="s">&quot;Residual: {0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">residual</span><span class="o">.</span><span class="n">Norm</span><span class="p">()))</span>

    <span class="n">structure</span><span class="o">.</span><span class="n">ExportVtkDataFileElements</span><span class="p">(</span><span class="s">&quot;Temperature1D_static.vtk&quot;</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Now that we have defined functions for creating and solving our problem, the last step (for the stationary case) is calling these functions:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[6]:
</div>
<div class="inner_cell">
    <div class="input_area">
<div class="highlight"><pre><span class="n">static_structure</span> <span class="o">=</span> <span class="n">create_structure</span><span class="p">()</span>
<span class="n">static_solve</span><span class="p">(</span><span class="n">static_structure</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
Residual: 1.07186614013e-13

</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We expect a linear distribution, basically <span class="math">\(u = k*q*x\)</span>, with the values <span class="math">\(u=0\)</span> at the left boundary, and <span class="math">\(u = 10\)</span> at the right boundary. Let's see:</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[7]:
</div>
<div class="inner_cell">
    <div class="input_area">
<div class="highlight"><pre><span class="k">print</span><span class="p">(</span><span class="n">static_structure</span><span class="o">.</span><span class="n">NodeGetTemperature</span><span class="p">(</span><span class="mi">0</span><span class="p">))</span>
<span class="k">print</span><span class="p">(</span><span class="n">static_structure</span><span class="o">.</span><span class="n">NodeGetTemperature</span><span class="p">(</span><span class="n">static_structure</span><span class="o">.</span><span class="n">GetNumNodes</span><span class="p">()</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
0.0
10.0

</pre>
</div>
</div>

</div>
</div>

</div>
<div class="cell border-box-sizing text_cell rendered">
<div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>For the transient solution, both boundaries will have a Dirichlet conditions, with a value according to the analytical solution mentioned earlier.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[8]:
</div>
<div class="inner_cell">
    <div class="input_area">
<div class="highlight"><pre><span class="k">def</span> <span class="nf">transient_solve</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>

    <span class="n">simulation_time</span> <span class="o">=</span> <span class="mf">1.8</span>
    <span class="n">newmark</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">NewmarkDirect</span><span class="p">(</span><span class="n">structure</span><span class="p">)</span>
    <span class="n">newmark</span><span class="o">.</span><span class="n">SetTimeStep</span><span class="p">(</span><span class="o">.</span><span class="mi">1</span><span class="o">*</span><span class="n">simulation_time</span><span class="p">)</span>
    <span class="n">newmark</span><span class="o">.</span><span class="n">SetToleranceForce</span><span class="p">(</span><span class="mf">1e-4</span><span class="p">)</span>
    <span class="n">newmark</span><span class="o">.</span><span class="n">SetAutomaticTimeStepping</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span>

    <span class="n">beta</span> <span class="o">=</span> <span class="mi">2</span><span class="o">*</span><span class="n">conductivity</span><span class="o">/</span><span class="p">(</span><span class="n">density</span><span class="o">*</span><span class="n">capacity</span><span class="p">)</span>
    <span class="c"># set dirichlet bc</span>
    <span class="n">boundary_temperature_east</span> <span class="o">=</span> <span class="n">analytic_solution</span><span class="p">(</span><span class="mf">0.0</span><span class="p">)</span>
    <span class="n">boundary_temperature_west</span> <span class="o">=</span> <span class="n">analytic_solution</span><span class="p">(</span><span class="n">length</span><span class="p">)</span>
    <span class="n">last_node_id</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">GetNumNodes</span><span class="p">()</span> <span class="o">-</span> <span class="mi">1</span>
    <span class="n">bc_west</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">ConstraintLinearSetTemperatureNode</span><span class="p">(</span><span class="n">last_node_id</span><span class="p">,</span> <span class="n">boundary_temperature_west</span><span class="p">)</span>
    <span class="n">bc_east</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">ConstraintLinearSetTemperatureNode</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">boundary_temperature_east</span><span class="p">)</span>

    <span class="n">end_temp_east</span> <span class="o">=</span> <span class="n">beta</span><span class="o">*</span><span class="n">simulation_time</span> <span class="o">+</span> <span class="n">boundary_temperature_east</span>
    <span class="n">temp_east</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">DoubleFullMatrix</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
    <span class="n">temp_east</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
    <span class="n">temp_east</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">simulation_time</span><span class="p">)</span>
    <span class="n">temp_east</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">boundary_temperature_east</span><span class="p">)</span>
    <span class="n">temp_east</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">end_temp_east</span><span class="p">)</span>
    <span class="n">newmark</span><span class="o">.</span><span class="n">AddTimeDependentConstraint</span><span class="p">(</span><span class="n">bc_east</span><span class="p">,</span> <span class="n">temp_east</span><span class="p">)</span>

    <span class="n">end_temp_west</span> <span class="o">=</span> <span class="n">beta</span><span class="o">*</span><span class="n">simulation_time</span> <span class="o">+</span> <span class="n">boundary_temperature_west</span>
    <span class="n">temp_west</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">DoubleFullMatrix</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
    <span class="n">temp_west</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mf">0.0</span><span class="p">)</span>
    <span class="n">temp_west</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">simulation_time</span><span class="p">)</span>
    <span class="n">temp_west</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">boundary_temperature_west</span><span class="p">)</span>
    <span class="n">temp_west</span><span class="o">.</span><span class="n">SetValue</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">end_temp_west</span><span class="p">)</span>
    <span class="n">newmark</span><span class="o">.</span><span class="n">AddTimeDependentConstraint</span><span class="p">(</span><span class="n">bc_west</span><span class="p">,</span> <span class="n">temp_west</span><span class="p">)</span>

    <span class="n">delete_directory</span> <span class="o">=</span> <span class="bp">True</span>
    <span class="n">newmark</span><span class="o">.</span><span class="n">SetResultDirectory</span><span class="p">(</span><span class="s">&quot;results_temp_1d&quot;</span><span class="p">,</span> <span class="n">delete_directory</span><span class="p">)</span>
    <span class="n">newmark</span><span class="o">.</span><span class="n">Solve</span><span class="p">(</span><span class="n">simulation_time</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">interpolate</span><span class="p">(</span><span class="n">structure</span><span class="p">,</span> <span class="n">function</span><span class="p">):</span>
    <span class="n">num_nodes</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">GetNumNodes</span><span class="p">()</span>

    <span class="n">coordinates</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">DoubleFullVector</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_nodes</span><span class="p">):</span>
        <span class="n">structure</span><span class="o">.</span><span class="n">NodeGetCoordinates</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">coordinates</span><span class="p">)</span>
        <span class="n">coordinates</span><span class="o">.</span><span class="n">convrtMatrixToNumpy</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="n">value</span> <span class="o">=</span> <span class="n">function</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">])</span>
        <span class="n">structure</span><span class="o">.</span><span class="n">NodeSetTemperature</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">structure</span>

<span class="k">def</span> <span class="nf">compare_to_analytic</span><span class="p">(</span><span class="n">structure</span><span class="p">):</span>
    <span class="n">num_nodes</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">GetNumNodes</span><span class="p">()</span>
    <span class="n">coordinates</span> <span class="o">=</span> <span class="n">nuto</span><span class="o">.</span><span class="n">DoubleFullVector</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
    <span class="n">x</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1</span><span class="p">))</span>
    <span class="n">exact_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">num_nodes</span><span class="p">)</span>
    <span class="n">fem_values</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">empty</span><span class="p">(</span><span class="n">num_nodes</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">num_nodes</span><span class="p">):</span>
        <span class="n">transient_structure</span><span class="o">.</span><span class="n">NodeGetCoordinates</span><span class="p">(</span><span class="n">i</span><span class="p">,</span> <span class="n">coordinates</span><span class="p">)</span>
        <span class="n">coordinates</span><span class="o">.</span><span class="n">convrtMatrixToNumpy</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>
        <span class="n">exact_values</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">analytic_solution</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">][</span><span class="mi">0</span><span class="p">],</span> <span class="mf">1.8</span><span class="p">)</span>
        <span class="n">fem_values</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">structure</span><span class="o">.</span><span class="n">NodeGetTemperature</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>

    <span class="n">errornorm</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">exact_values</span> <span class="o">-</span> <span class="n">fem_values</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="n">np</span><span class="o">.</span><span class="n">linalg</span><span class="o">.</span><span class="n">norm</span><span class="p">(</span><span class="n">exact_values</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">errornorm</span>
</pre></div>

</div>
</div>
</div>

</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">
In&nbsp;[9]:
</div>
<div class="inner_cell">
    <div class="input_area">
<div class="highlight"><pre><span class="n">transient_structure</span> <span class="o">=</span> <span class="n">create_structure</span><span class="p">(</span><span class="n">number_of_time_derivatives</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">transient_structure</span> <span class="o">=</span> <span class="n">interpolate</span><span class="p">(</span><span class="n">transient_structure</span><span class="p">,</span> <span class="n">analytic_solution</span><span class="p">)</span>
<span class="n">transient_solve</span><span class="p">(</span><span class="n">transient_structure</span><span class="p">)</span>

<span class="n">error</span> <span class="o">=</span> <span class="n">compare_to_analytic</span><span class="p">(</span><span class="n">transient_structure</span><span class="p">)</span>
<span class="k">print</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
</pre></div>

</div>
</div>
</div>

<div class="output_wrapper">
<div class="output">


<div class="output_area"><div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>
3.35500785856e-15

</pre>
</div>
</div>

</div>
</div>

</div>
